<?php 



class Place extends Eloquent {


	/**
	 * The database table used by the model.
	 *
	 * @var string
	 */
	protected $table = 'places';
	protected $primaryKey = 'id';
	const CREATED_AT = 'created_time';
	const UPDATED_AT = 'updated_time';


	public function placeInformation(){
		return $this->hasOne('PlaceInformation','id','id');
	}

	public function ID(){
		return $this->belongsTo('ID','id');
	}

	public function rates(){
		return $this->hasMany('UserRatePlace','place_id');
	}

	public function permissions(){
		return $this->hasMany('UserHasPlace', 'place_id');
	}

	public function updatedBy(){
		return $this->belongsTo('User','updated_by');
	}

	public function createdBy(){
		return $this->belongsTo('User','created_by');
	}

	public function workAtHere(){
		return $this->hasMany('UserInformation', 'work_place_id');
	}

	public function studyAtThisHighschool(){
		return $this->hasMany('UserInformation', 'highschool_place_id');
	}

	public function studyAtThisCollege(){
		return $this->hasMany('UserInformation', 'college_place_id');
	}

	public function getDistance($latitude = 0, $longitude = 0){
		$circumference = 400075.6;
		$distance = 0.0;
		$latitude1Rad = deg2rad($this->latitude);
		$latitude2Rad = deg2rad($latitude);
		$longitude1Rad = deg2rad($this->longitude);
		$longitude2Rad = deg2rad($longitude);

		$longitudeDiff = $longitude1Rad-$longitude2Rad;

		$angleCalculation = acos(sin($latitude1Rad)*sin($latitude2Rad)+cos($latitude1Rad)*cos($latitude2Rad)*cos($longitudeDiff));

		$distance = rad2deg($angleCalculation);
		$distanceMiles = $distance*60*1.1515; //miles
		$distanceKm = $distanceMiles * 1.609344;
		return $distanceKm;

	}
}
